#include "GY_SystemClock.h"
#include <QDateTime>
#include <QDebug>
#include <QLocale>

GY_SystemClock::GY_SystemClock(const QString &guid, QGraphicsItem *parent)
    :GY_BasicsCalculators(guid,this,parent)
{
    //图元类型
    GY_Object::setUserData(GY_ENUM::ClassType,QVariant("SystemClock"));
    initializationPropertyInfo();
    m_pTimer = new QTimer(this);
    connect(m_pTimer, &QTimer::timeout, [=]{
        QDateTime datetime;
        //获取当前日期时间
        datetime = QDateTime::currentDateTime();
        //设置日期时间转化语言
        QLocale::setDefault(QLocale(QLocale::C,QLocale::UnitedStates));
        //输出时间格式
        m_PropertyInfoMap["CurrentTime"].propertyValue = QLocale().toString(datetime,"hh:mm:ss");
        m_PropertyInfoMap["CurrentDate"].propertyValue = QLocale().toString(datetime,"dddd, MMMM dd, yyyy");
//        qDebug()<<m_PropertyInfoMap["CurrentTime"].propertyValue.toString()<<m_PropertyInfoMap["CurrentDate"].propertyValue.toString();
    });
    m_pTimer->start(1000);
}

void GY_SystemClock::initializationPropertyInfo()
{
    GY_PropertyInfo ObjectName(this);
    ObjectName.propertyName = "ObjectName";
    ObjectName.propertyValue = QString();
    ObjectName.updateType = Update;
    ObjectName.propertyType = QStringPropertyType;
    ObjectName.updatePropertyFun = (GY_PropertyInfo::UpdatePropertyFun)&GY_SystemClock::updateObjectName;
    m_PropertyInfoMap.insert(ObjectName.propertyName,ObjectName);
    m_PropertyInfoMap[ObjectName.propertyName].setConnect();

    GY_PropertyInfo CurrentTimeCapacity(this);
    CurrentTimeCapacity.propertyName = "CurrentTime.Capacity";
    CurrentTimeCapacity.propertyValue = 127;
    CurrentTimeCapacity.updateType = Update;
    CurrentTimeCapacity.propertyType = IntPropertyType;
    CurrentTimeCapacity.updatePropertyFun = (GY_PropertyInfo::UpdatePropertyFun)&GY_SystemClock::updateCurrentTimeCapacity;
    m_PropertyInfoMap.insert(CurrentTimeCapacity.propertyName,CurrentTimeCapacity);
    m_PropertyInfoMap[CurrentTimeCapacity.propertyName].setConnect();

    GY_PropertyInfo CurrentTime(this);
    CurrentTime.propertyName = "CurrentTime";
    CurrentTime.propertyValue = QString();
    CurrentTime.updateType = Update;
    CurrentTime.propertyType = QStringPropertyType;
    CurrentTime.updatePropertyFun = (GY_PropertyInfo::UpdatePropertyFun)&GY_SystemClock::updateCurrentTime;
    m_PropertyInfoMap.insert(CurrentTime.propertyName,CurrentTime);
    m_PropertyInfoMap[CurrentTime.propertyName].setConnect();

    GY_PropertyInfo CurrentDateCapacity(this);
    CurrentDateCapacity.propertyName = "CurrentDate.Capacity";
    CurrentDateCapacity.propertyValue = 127;
    CurrentDateCapacity.updateType = Update;
    CurrentDateCapacity.propertyType = IntPropertyType;
    CurrentDateCapacity.updatePropertyFun = (GY_PropertyInfo::UpdatePropertyFun)&GY_SystemClock::updateCurrentDateCapacity;
    m_PropertyInfoMap.insert(CurrentDateCapacity.propertyName,CurrentDateCapacity);
    m_PropertyInfoMap[CurrentDateCapacity.propertyName].setConnect();

    GY_PropertyInfo CurrentDate(this);
    CurrentDate.propertyName = "CurrentDate";
    CurrentDate.propertyValue = QString();
    CurrentDate.updateType = Update;
    CurrentDate.propertyType = QStringPropertyType;
    CurrentDate.updatePropertyFun = (GY_PropertyInfo::UpdatePropertyFun)&GY_SystemClock::updateCurrentDate;
    m_PropertyInfoMap.insert(CurrentDate.propertyName,CurrentDate);
    m_PropertyInfoMap[CurrentDate.propertyName].setConnect();

    GY_PropertyInfo Frequency(this);
    Frequency.propertyName = "Frequency";
    Frequency.propertyValue = 1;
    Frequency.updateType = Update;
    Frequency.propertyType = IntPropertyType;
    Frequency.updatePropertyFun = (GY_PropertyInfo::UpdatePropertyFun)&GY_SystemClock::updateFrequency;
    m_PropertyInfoMap.insert(Frequency.propertyName,Frequency);
    m_PropertyInfoMap[Frequency.propertyName].setConnect();
}

bool GY_SystemClock::updateCurrentTimeCapacity(const QString &, const QVariant &)
{
    return false;
}

bool GY_SystemClock::updateCurrentTime(const QString &, const QVariant &)
{
    return false;
}

bool GY_SystemClock::updateCurrentDateCapacity(const QString &, const QVariant &)
{
    return false;
}

bool GY_SystemClock::updateCurrentDate(const QString &, const QVariant &)
{
    return false;
}

bool GY_SystemClock::updateFrequency(const QString &name, const QVariant &value)
{
    if(!value.canConvert<int>()) {
        return false;
    }
    m_PropertyInfoMap[name].propertyValue = value;
    return true;
}
